<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="订单状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="配送方式" prop="addressType">
        <el-select
          v-model="queryParams.addressType"
          placeholder="配送方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.address_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收货人" prop="deliveryName">
        <el-input
          v-model="queryParams.deliveryName"
          placeholder="请输入游客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人手机号" prop="deliveryPhone">
        <el-input
          v-model="queryParams.deliveryPhone"
          placeholder="请输入游客手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单日期" prop="orderTime">
        <el-date-picker
          v-model="queryParams.orderTime"
          type="date"
          clearable
          value-format="yyyy-MM-dd"
          placeholder="选择日期"/>
      </el-form-item>
<!--      <el-form-item label="订单分类" prop="orderType">-->
<!--        <el-select-->
<!--          v-model="queryParams.orderType"-->
<!--          placeholder="订单分类"-->
<!--          clearable-->
<!--        >-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.order_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="orderList" >
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="订单号" align="center" prop="orderNum" min-width="120" show-overflow-tooltip/>
      <el-table-column label="收货人" align="center" prop="deliveryName" width="100"/>
      <el-table-column label="收货人手机号" align="center" prop="deliveryPhone" width="110"/>
      <el-table-column label="配送方式" align="center" prop="addressType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.address_type" :value="scope.row.addressType"/>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="addressDetail" min-width="230" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{handleAddressDetail(scope.row.addressDetail)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="总金额" align="center" prop="orderTotalPrice" width="100">
        <template v-slot="scope">
          <span>{{(scope.row.orderTotalPrice / 100).toFixed(2)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="150"/>
      <el-table-column label="状态" align="center" prop="orderStatus" width="80">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="orderPayType" width="80">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.pay_way" :value="scope.row.orderPayType"/>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="130">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if='scope.row.orderStatus==1'
            @click="handleCancel(scope.row)"
            v-hasPermi="['order:order:edit']"
          >订单取消</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-money"
            v-if='scope.row.orderStatus!=1 && scope.row.orderStatus!=7 && scope.row.orderStatus!=6 && scope.row.orderStatus!=3 && scope.row.orderPayType==1'
            @click="handleMoney(scope.row)"
            v-hasPermi="['pay:refundOrder:refundOrder']"
          >订单退款</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-office-building"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:order:remove']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div class="clearfix">
        <b>订单信息</b>
      </div>
      <el-row class="list-group list-group-striped">
        <el-col :span="11">
          <li class="list-group-item">
            <svg-icon icon-class="number" /> <b>订单号</b>
            <div class="pull-right">{{ form.orderNum }}</div>
          </li>
        </el-col>
        <el-col :span="11" class="pull-right">
          <li class="list-group-item">
            <svg-icon icon-class="user" /> <b>收货人</b>
            <div class="pull-right">{{ form.deliveryName }}</div>
          </li>
        </el-col>
        <el-col :span="11" class="pull-right">
          <li class="list-group-item">
            <svg-icon icon-class="phone" /> <b>收货人手机号</b>
            <div class="pull-right">{{ form.deliveryPhone }}</div>
          </li>
        </el-col>
        <el-col :span="11">
          <li class="list-group-item">
            <svg-icon icon-class="question" /> <b>配送类型</b>
            <div class="pull-right">
              <dict-tag :options="dict.type.address_type" :value="form.addressType"/>
            </div>
          </li>
        </el-col>
        <el-col :span="11">
          <li class="list-group-item">
            <svg-icon icon-class="money" /> <b>总金额</b>
            <div class="pull-right">{{ (form.orderTotalPrice / 100).toFixed(2) }}</div>
          </li>
        </el-col>
        <el-col :span="11" class="pull-right">
          <li class="list-group-item">
            <svg-icon icon-class="date" /> <b>下单时间</b>
            <div class="pull-right">{{ form.orderTime }}</div>
          </li>
        </el-col>
        <el-col :span="11">
          <li class="list-group-item">
            <svg-icon icon-class="money" /> <b>邮费</b>
            <div class="pull-right">{{ (form.postagePrice / 100).toFixed(2) }}</div>
          </li>
        </el-col>
        <el-col :span="11" class="pull-right">
          <li class="list-group-item">
            <svg-icon icon-class="switch" /> <b>状态</b>
            <div class="pull-right">
              <dict-tag :options="dict.type.order_status" :value="form.orderStatus"/>
            </div>
          </li>
        </el-col>
        <el-col :span="11">
          <li class="list-group-item">
            <svg-icon icon-class="phone" /> <b>支付方式</b>
            <div class="pull-right">
              <dict-tag :options="dict.type.pay_way" :value="form.orderPayType"/>
            </div>
          </li>
        </el-col>
        <el-col :span="11" class="pull-right">
          <li class="list-group-item">
            <svg-icon icon-class="date" /> <b>支付时间</b>
            <div class="pull-right">{{ form.payTime }}</div>
          </li>
        </el-col>
        <el-col :span="24">
          <li class="list-group-item">
            <svg-icon icon-class="international" /> <b>详细地址</b>
            <div class="pull-right">{{handleAddressDetail(form.addressDetail)}}</div>
          </li>
        </el-col>
      </el-row>

      <div class="clearfix">
        <b>订单明细</b>
      </div>
      <detailsChild ref='detailsChildRefGet' :pOrderId="orderId"/>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder ,cancelOrder,refundOrder,getOrderDetail} from "@/api/order/order";
import detailsChild from './detail'
export default {
  name: "Order",
  components:{
    detailsChild
  },
  dicts:['order_status','order_type','pay_way','address_type'],
  data() {
    return {
      // 退款按钮状态
      tkLodaing:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        orderTname: null,
        orderTphone: null,
        orderTcode: null,
        orderTime: null,
        orderStatus: null,
        orderPayType: null,
        orderTotalPrice: null,
        userId: null,
        orderAddressId: null,
        postagePrice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      orderId: null
    };
  },
  created() {
    this.queryParams.orderType = '4';
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNum: null,
        orderTname: null,
        orderTphone: null,
        orderTcode: null,
        orderTime: null,
        orderStatus: null,
        orderPayType: null,
        orderTotalPrice: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        userId: null,
        orderAddressId: null,
        postagePrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    // 详情弹框
    detailDialog(row){
      console.log(this.$refs.detailsChildRefGet)
      this.$refs.detailsChildRefGet.isOpen(row.orderId)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrderDetail(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单";
        this.orderId = response.data.orderId
      });
    },
    // 取消订单
    handleCancel(row){
      const orderIds = row.orderId || this.ids;
      console.log(orderIds,row)
      this.$modal.confirm('是否确认取消订单编号为"' + orderIds + '"的数据项？').then(function() {
        return cancelOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消订单成功");
      }).catch(() => {});
    },
    // 订单退款
    handleMoney(row){
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认退款订单编号为"' + orderIds + '"的数据项？').then(function() {
        // this.tkLodaing=true
        console.log(orderIds)
        return refundOrder(orderIds);
      }).then(() => {
        // this.tkLodaing=false
        this.getList();
        this.$modal.msgSuccess("订单退款成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    handleAddressDetail(val) {
      if (val) {
        let res = val.split("@@");
        if (res.length > 1) {
          return res[0] + "—" + res[1]
        }
        return val;
      }
    }
  }
};
</script>
<style lang="scss">

.clearfix {
  &:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
    margin-bottom: 10px;
  }
}
</style>
